<?php
namespace modules\contact;
use \modules\ui;
use \modules\potential\page as potential;
use \modules\auth\api as auth;
use \phiction\array_utils as arr;

class page
{
    static function mapping()
    {
        return [
            '/add' => 'add',
            '/active/#page' => 'active',
            '/archived/#page' => 'archived',
            '/show/#id' => 'show',
            '/edit/#id' => 'edit',
        ];
    }

    static function add($q, $html, &$args)
    {
        $user = auth::validate($q, $args);
        $orgname = $user['orgname'];

        $args['SUBTITLE'] = "添加联系人";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html);

        return ui::card(ui::form($html, '/api/contact/add',
            ui::icon_label('添加联系人', 'user-plus'), [
                ['text'    , '隶属组织' , $orgname, 'readonly' => true],
                ['text'    , '姓名'     , 'name' => 'name'   , 'maxlength' => 64] ,
                ['enum'    , '性别'     , 'name' => 'gender' , ['man', 'man' => '先生', 'woman' => '女士']],
                ['email'   , '邮箱'     , 'name' => 'email'  , 'maxlength' => 64] ,
                ['text'    , '联系电话1', 'name' => 'phone1' , 'maxlength' => 64] ,
                ['text'    , '联系地址1', 'name' => 'addr1'  , 'maxlength' => 256],
                ['text'    , '联系电话2', 'name' => 'phone2' , 'maxlength' => 64  , null, true],
                ['text'    , '联系地址2', 'name' => 'addr2'  , 'maxlength' => 256 , null, true],
                ['date'    , '生日'     , 'name' => 'birthdate'],
                ['longtext', '备注'     , 'name' => 'comment', null, true],
            ], [
                [ui::icon_label('返回有效联系人列表', 'angle-left'), '/contact/active/1'],
            ], '/contact/active/1'),
        "添加联系人");
    }

    static function active($q, $html, &$args)
    {
        $page = arr::get_or_bad_request($args, 'page');
        $list = api::list_active($q, $args);

        $args['SUBTITLE'] = "有效联系人";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html, [
            [ui::icon_label('添加联系人', 'user-plus'), '/contact/add'],
        ]);

        foreach ($list as &$item) {
            $id     = $item['id'];
            $name   = $item['name'];
            $gender = ['man' => '先生', 'woman' => '女士'][$item['gender']];
            $phone1 = $item['phone1'];
            $addr1  = $item['addr1'];
            $item = [
                "$name $gender", "$phone1 $addr1", "href" => "/contact/show/$id",
                [
                    [ui::icon_label('编辑资料', 'edit'), "/contact/edit/$id"],
                    [ui::icon_label('尘封', 'ban'), "data-api" => "/api/contact/archive", "data-api-id" => $id],
                ],
            ];
        }

        $pager = ui::pager(function ($page) {
            return "/contact/active/$page";
        }, $page, count($list) === 10);

        return ui::card(['.', ui::list($list), $pager], "有效联系人");
    }

    static function archived($q, $html, &$args)
    {
        $page = arr::get_or_bad_request($args, 'page');
        $list = api::list_archived($q, $args);

        $args['SUBTITLE'] = "已尘封联系人";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html, [
            [ui::icon_label('添加联系人', 'user-plus'), '/contact/add'],
        ]);

        foreach ($list as &$item) {
            $id     = $item['id'];
            $name   = $item['name'];
            $gender = ['man' => '先生', 'woman' => '女士'][$item['gender']];
            $phone1 = $item['phone1'];
            $addr1  = $item['addr1'];
            $item = [
                "$name $gender", "$phone1 $addr1", "href" => "/contact/show/$id",
                [
                    [ui::icon_label('解封', 'check-circle-o'), "data-api" => "/api/contact/active", "data-api-id" => $id],
                ],
            ];
        }

        $pager = ui::pager(function ($page) {
            return "/contact/archived/$page";
        }, $page, count($list) === 10);

        return ui::card(['.', ui::list($list), $pager], "已尘封联系人");
    }

    static function edit($q, $html, &$args)
    {
        $user    = auth::validate($q, $args);
        $orgname = $user['orgname'];

        $args['SUBTITLE'] = "修改联系人资料";

        $profile   = api::contact_profile($q, $args);
        $name      = $profile['name'];
        $gender    = $profile['gender'];
        $email     = $profile['email'];
        $addr1     = $profile['addr1'];
        $phone1    = $profile['phone1'];
        $phone2    = $profile['phone2'];
        $addr2     = $profile['addr2'];
        $birthdate = $profile['birthdate'];
        $comment   = $profile['comment'];
        $id        = $profile['id'];
        $active    = $profile['active'];

        return ui::card(ui::form($html, '/api/contact/edit',
            ui::icon_label('确认修改', 'edit'), [
                ['hidden'  , ''         , $id       , 'name' => 'id'],
                ['text'    , '隶属组织' , $orgname  , 'readonly' => true],
                ['text'    , '姓名'     , $name     , 'name' => 'name'      , 'maxlength' => 64],
                ['enum'    , '性别'     , 'name' => 'gender' , [$gender, 'man' => '先生', 'woman' => '女士']],
                ['email'   , '邮箱'     , $email    , 'name' => 'email'     , 'maxlength' => 64],
                ['text'    , '联系电话1', $phone1   , 'name' => 'phone1'    , 'maxlength' => 64],
                ['text'    , '联系地址1', $addr1    , 'name' => 'addr1'     , 'maxlength' => 256],
                ['text'    , '联系电话2', $phone2   , 'name' => 'phone2'    , 'maxlength' => 64 , true],
                ['text'    , '联系地址2', $addr2    , 'name' => 'addr2'     , 'maxlength' => 256, true],
                ['date'    , '生日'     , $birthdate, 'name' => 'birthdate'],
                ['longtext', '备注'     , $comment  , 'name' => 'comment'   , true],
            ], [
                [ui::icon_label('返回联系人资料', 'angle-left'), "/contact/show/$id"],
            ], "/contact/show/$id"),
        "修改联系人资料");
    }

    static function show($q, $html, &$args)
    {
        $user    = auth::validate_with_role($q, $args, 'salesman');
        $orgname = $user['orgname'];

        $profile   = api::contact_profile($q, $args);
        $name      = $profile['name'];
        $gender    = $profile['gender'];
        $email     = $profile['email'];
        $addr1     = $profile['addr1'];
        $phone1    = $profile['phone1'];
        $phone2    = $profile['phone2'];
        $addr2     = $profile['addr2'];
        $birthdate = $profile['birthdate'];
        $comment   = $profile['comment'];
        $id        = $profile['id'];
        $active    = $profile['active'];

        $potentials = potential::list_all($q, $html, $args);

        $args['SUBTITLE'] = "联系人资料";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html, [
            [ui::icon_label('添加商机', 'plus'), "/potential/add/$id"],
            [ui::icon_label('编辑联系人资料', 'edit'), "/contact/edit/$id"],
        ]);

        $args['id'] = $id;
        return $html->mark(['.',
            ui::card(ui::readonly_form($html,
            [
                ['text'    , '隶属组织' , $orgname],
                ['text'    , '姓名'     , $name]   ,
                ['enum'    , '性别'     , [$gender, 'man' => '先生', 'woman' => '女士'], 'name' => 'gender'],
                ['email'   , '邮箱'     , $email]    ,
                ['text'    , '联系电话1', $phone1]   ,
                ['text'    , '联系地址1', $addr1]    ,
                ['text'    , '联系电话2', $phone2]   ,
                ['text'    , '联系地址2', $addr2]    ,
                ['date'    , '生日'     , $birthdate],
                ['longtext', '备注'     , $comment]  ,
                ['enum'    , '状态'     , [$active ? 't' : 'f', 't' => '有效', 'f' => '尘封'], 'name' => 'active'] ,
            ]), "联系人 $name 的资料"),
            $potentials,
        ]);
    }
}

